package com.hrb.douban.adapter;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.hrb.douban.CustomApplication;
import com.hrb.douban.R;
import com.hrb.douban.record.FilmDetailRecord;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

public class CollectMovieAdapter extends RecyclerView.Adapter<CollectMovieAdapter.CHolder> {

    private ArrayList list;

    public CollectMovieAdapter(ArrayList list) {
        this.list = list;
    }

    @NonNull
    @Override
    public CHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.collect_movie_item_layout, viewGroup, false);
        CHolder cHolder = new CHolder(view);
        return cHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull CHolder cHolder, int i) {
        try {
            if(FilmDetailRecord.class.equals(list.get(i).getClass())){
                FilmDetailRecord record=(FilmDetailRecord) list.get(i);
                Glide.with(CustomApplication.getContext()).load(record.getImages().getLarge()).centerCrop().into(cHolder.filmCover);
                //ImageUtil.loadIntoUseFitWidth(record.getImages().getLarge(), cHolder.filmCover);
                cHolder.fTitle.setText(record.getTitle());
                String dirStr = "";
                String castStr = "";
                for (FilmDetailRecord.DirectorsBean der : record.getDirectors()) {
                    dirStr += der.getName() + "/";
                    ;
                }
                for (FilmDetailRecord.CastsBean cast : record.getCasts()) {
                    castStr += cast.getName() + "/";
                }
                cHolder.fDirector.setText("导演:" + dirStr.substring(0, dirStr.lastIndexOf("/")));
                cHolder.fRate.setText("豆瓣评分 : " + record.getRating().getAverage());
                cHolder.fCast.setText("演员 : " + castStr.substring(0, castStr.lastIndexOf("/")));
                cHolder.fYear.setText("上映年份 : " + record.getYear());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public class CHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.film_cover)
        ImageView filmCover;
        @BindView(R.id.f_title)
        TextView fTitle;
        @BindView(R.id.f_director)
        TextView fDirector;
        @BindView(R.id.f_cast)
        TextView fCast;
        @BindView(R.id.f_rate)
        TextView fRate;
        @BindView(R.id.f_year)
        TextView fYear;
        public CHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
        }
    }
}
